<template>
  <!-- 精选题库 Table 组件 -->
        <!-- @cell-mouse-enter.once="rowDrop" -->
  <div>
    <el-table
      ref="tableData"
      :data="tableData.items"
      row-key="id"
      style="width: 100%"
      :header-cell-style="{
        background: '#FAFAFA',
        color: '#909399',
        borderBottom: '2px solid #e8e8e8',
      }"
    >
      <el-table-column
        prop="number"
        width="120px"
        label="试题编号"
      ></el-table-column>
      <el-table-column
        width="120px"
        prop="subjectID"
        label="学科"
        :formatter="formatterSubjectID"
      ></el-table-column>
      <el-table-column width="120px" prop="catalog" label="目录">
      </el-table-column>
      <el-table-column
        width="120px"
        prop="questionType"
        label="题型"
        :formatter="formatterQuestionType"
      >
      </el-table-column>
      <el-table-column
        prop="question"
        label="题干"
        width="280px"
        :formatter="formatterQuestion"
      >
      </el-table-column>
      <el-table-column prop="addDate" label="录入时间" width="180px">
        <template slot-scope="{ row }">
          {{ row.addDate | parseTimeByString }}
        </template>
      </el-table-column>
      <el-table-column
        prop="difficulty"
        label="难度"
        width="80px"
        :formatter="formatterDifficulty"
      >
      </el-table-column>
      <el-table-column width="120px" prop="creator" label="录入人">
      </el-table-column>
      <el-table-column
        width="120px"
        prop="chkState"
        label="审核状态"
        :formatter="formatterChkState"
      >
      </el-table-column>
      <el-table-column prop="chkRemarks" label="审核意见" width="150px">
      </el-table-column>
      <el-table-column width="120px" prop="chkUser" label="审核人">
      </el-table-column>
      <el-table-column
        width="120px"
        prop="publishState"
        label="发布状态"
        :formatter="formatterPublishState"
      >
      </el-table-column>
      <el-table-column
        prop="address"
        label="操作"
        fixed="right"
        width="200px"
        align="center"
      >
        <template slot-scope="{ row }">
          <el-button type="text" @click="btnView(row)">预览</el-button>
          <el-button
            type="text"
            :disabled="row.chkState === 1 || row.chkState === 2 ? true : false"
            @click="btnCheck(row)"
            >审核</el-button
          >
          <el-button
            type="text"
            :disabled="row.publishState === 1 ? true : false"
            @click="$router.push(`/questions/new?id=${row.id}&type='shr'`)"
            >修改</el-button
          >
          <el-button type="text" @click="btnPutaway(row)">{{
            comPutaway(row)
          }}</el-button>
          <el-button
            type="text"
            :disabled="row.publishState === 1 ? true : false"
            @click="btnDel(row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-dialog
      :visible.sync="showCheck"
      title="题目审核"
      width="400px"
      @close="btnCancel"
    >
      <el-form>
        <el-form-item>
          <el-radio v-model="checkForm.chkState" :label="1">通过</el-radio>
          <el-radio v-model="checkForm.chkState" :label="2">拒绝</el-radio>
        </el-form-item>
      </el-form>
      <el-form>
        <el-form-item>
          <el-input
            type="textarea"
            rows="4"
            v-model="checkForm.chkRemarks"
            placeholder="请输入审核意见"
          ></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <el-row>
          <el-button size="medium" @click="btnCancel">取消</el-button>
          <el-button size="medium" type="primary" @click="btnOk"
            >确认</el-button
          >
        </el-row>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import {
  detail as quesDetail,
  choiceCheck,
  remove,
  choicePublish
} from '../../api/hmmm/questions'
import { html2Text } from '../../utils/index'
import * as baseConstanst from '../../api/hmmm/constants'
import Sortable from 'sortablejs'

export default {
  props: {
    tableData: {
      type: Object,
      default: () => {}
    },
    isShowMyDialog: {
      type: Boolean,
      default: false
    },
    subjectSimple: {
      type: Array,
      default: () => []
    }
  },
  data () {
    return {
      showCheck: false, // 控制审核
      viewData: {}, // 题目预览详情
      checkForm: {
        chkState: 1,
        chkRemarks: '',
        id: ''
      }
    }
  },
  mounted () {
    this.rowDrop()
  },
  methods: {
    // 题干返回的是带标签的数据, 需要格式化一下
    // 这里还需要优化的一点就是代码的高亮显示
    formatterQuestion (row, column, cellValue, index) {
      // console.log(cellValue)
      return html2Text(cellValue)
    },
    // 发布状态 这里应该需要根据审核状态去做判断 chkState 待审核0 已审核1 已拒绝2
    formatterPublishState (row, column, cellValue, index) {
      // baseConstanst.publishType
      // console.log(row.chkState)
      // const obj = baseConstanst.choicenessPublishType.find(
      //   (item) => Number(item.value) === Number(cellValue)
      // )
      if (row.chkState === 0 || row.chkState === 2) return '待发布'
      if (row.chkState === 1) return cellValue === 1 ? '已发布' : '已下架'
      // if (row.chkState === 2)
      // return obj?.label ? obj.label : '待发布'
    },
    // 格式化学科
    formatterSubjectID (row, column, cellValue, index) {
      const obj = this.subjectSimple.find((item) => cellValue === item.value)
      return obj?.label || '未知'
    },
    // 格式化题型
    formatterQuestionType (row, column, cellValue, index) {
      const obj = baseConstanst.questionType.find(
        (item) => Number(cellValue) === item.value
      )
      return obj?.label || '未知'
    },
    // 格式化难度
    formatterDifficulty (row, column, cellValue, index) {
      const obj = baseConstanst.difficulty.find(
        (item) => Number(cellValue) === item.value
      )
      return obj?.label || '未知'
    },
    // 格式化审核状态
    formatterChkState (row, column, cellValue, index) {
      const obj = baseConstanst.choicenessChkType.find(
        (item) => Number(cellValue) === item.value
      )
      return obj?.label || '未知'
    },

    // 预览
    async btnView (row) {
      // console.log(row.id)
      const { data } = await quesDetail({ id: row.id })
      console.log(data)
      this.$emit('update:viewData', data)
      this.$emit('update:isShowMyDialog', true)
      // console.log(this.viewData)
    },
    // 审核
    btnCheck (row) {
      // console.log(row)
      this.showCheck = true
      this.checkForm.id = row.id
    },
    // 审核确认
    async btnOk () {
      try {
        if (!this.checkForm.chkRemarks) {
          return this.$message.warning('请输入审核意见')
        }
        await choiceCheck(this.checkForm)
        this.$emit('getChoice')
        this.$message.success('审核成功')
      } catch (error) {
        console.log(error)
      }
      this.showCheck = false
    },
    btnCancel () {
      this.checkForm.chkRemarks = ''
      this.showCheck = false
    },
    // 删除
    async btnDel (row) {
      try {
        await this.$confirm('此操作将永久删除该题目, 是否继续?', '提示', {
          type: 'warning'
        })
        await remove(row)
        await this.$message.success('删除成功')
        await this.$emit('getDelChoice')
      } catch (error) {
        console.log(error)
      }
    },
    // 上下架
    async btnPutaway (row) {
      try {
        // 如果发布状态为1 说明已经上架了 ,此时需要执行下架的操作, 并执行下架
        if (row.publishState) {
          await this.$confirm('确定下架这道题目吗?', '提示', {
            type: 'warning'
          })
          await choicePublish({ ...row, publishState: 0 })
          await this.$message.success('下架成功')
        } else {
          await this.$confirm('确定上架这道题目吗?', '提示', {
            type: 'warning'
          })
          await choicePublish({ ...row, publishState: 1 })
          await this.$message.success('上架成功')
        }
        this.$emit('getChoice')
      } catch (error) {
        console.log(error)
      }
    },
    comPutaway (row) {
      return row.publishState === 1 ? '下架' : '上架'
    },
    // 行拖拽
    rowDrop () {
      const tbody2 = this.$refs.tableData.$el.querySelector(
        '.el-table__body-wrapper tbody'
      )
      Sortable.create(tbody2, {
        animation: 180,
        onEnd: ({ newIndex, oldIndex }) => {
          // console.log('newIndex', newIndex)
          // console.log('oldIndex', oldIndex)
          const currRow = this.tableData.items.splice(oldIndex, 1)[0]
          this.tableData.items.splice(newIndex, 0, currRow)
        }
      })
    }
  }
}
</script>

<style lang="scss">
.container {
  padding: 10px;
}
.btn_wrapper {
  margin-bottom: 15px;
}
.my-input {
  height: 32px;
  line-height: 32px;
}
.el-button {
  font-size: 12px;
}
// .hover-row > td {
//   background-color: #fff !important;
// }

// .sortable-chosen > td {
//   // 拖动的样式
//   background-color: #eff2f6 !important;
// }

// .el-table--enable-row-hover .el-table__body tr:hover > td {
//   // 修复拖拽的时候hover的不消失的问题
//   background-color: #fff;
// }

</style>
